<template>


  <div class="add-space" style="width: 60%;margin:auto;">
    <a-form layout="vertical" :model="formData" @finish="handleSubmit">
      <a-form-item label="空间名称" name="spaceName">
        <a-input v-model:value="formData.spaceName" placeholder="请输入空间名称" allow-clear />
      </a-form-item>
      <a-form-item label="空间级别" name="spaceLevel">
        <a-select
          v-model:value="formData.spaceLevel"
          :options="SPACE_LEVEL_OPTIONS"
          placeholder="请输入空间级别"
          style="min-width: 180px"
          allow-clear
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%" :loading="loading">
          提交
        </a-button>
      </a-form-item>
    </a-form>

    <a-card title="空间级别介绍">
      <a-typography-paragraph>
        * 目前仅支持开通普通版，如需升级空间，请联系
        <a href="" target="_blank">管理员</a>。
      </a-typography-paragraph>
      <a-typography-paragraph v-for="spaceLevel in spaceLevelList">
        {{ spaceLevel.text }}： 大小 {{ spaceLevel.maxSize }}， 数量 {{ spaceLevel.maxCount }}
      </a-typography-paragraph>
    </a-card>


  </div>

</template>


<script setup lang="ts">
import { SPACE_LEVEL_OPTIONS, SPACE_LEVEL_ENUM } from '@/constants/space.ts'
import { onMounted, reactive, ref } from 'vue'
import {
  addSpaceUsingPost,
  getSpaceByIdUsingGet,
  listSpaceLevelUsingGet,
  updateSpaceUsingPost
} from '@/api/spaceController.ts'
import { message } from 'ant-design-vue'
import { useRoute, useRouter } from 'vue-router'

//
const formData = reactive<API.SpaceAddRequest | API.SpaceUpdateRequest>({
  spaceName: '',
  spaceLevel: SPACE_LEVEL_ENUM.COMMON
})
const loading = ref(false)

const router = useRouter()
// 提交表单
const handleSubmit = async (value: any) => {
  loading.value = true
  const spaceId = oldSpace.value?.id
  let res
  if (spaceId) {
    res = await updateSpaceUsingPost({
      id: spaceId,
      ...formData
    })
  } else {
    res = await addSpaceUsingPost({
      ...formData
    })
  }

  if (res.data.code === 0 && res.data.data) {
    message.success('操作成功！')
    // console.log(res.data.data)
    let path = `/space/${spaceId ?? res.data.data}`
    await router.push({
      path
    })

  } else {
    message.error('操作失败！' + res.data.message)
  }

  loading.value = false
}

const spaceLevelList = ref<API.SpaceLevel>([])

const fetchSpaceLevelList = async () => {
  const res = await listSpaceLevelUsingGet()
  if (res.data.code === 0 && res.data.data) {
    // console.log(res)
    spaceLevelList.value = res.data.data

  }
}

const route = useRoute()
const oldSpace = ref<API.SpaceVO>()

// 获取编辑空间的数据
const fetchEditDate = async () => {

  const id = route.query?.id
  console.log(id)
  if (!id) return
  const res = await getSpaceByIdUsingGet({
    id: id
  })
  if (res.data.code === 0 && res.data.data) {
    oldSpace.value = res.data.data
    formData.spaceName = res.data.data.spaceName
    formData.spaceLevel = res.data.data.spaceLevel
  }

}
onMounted(() => {
  fetchSpaceLevelList()
  fetchEditDate()
})
</script>

<style scoped>

</style>
